from flask import Blueprint, render_template, jsonify, request
from models.entity.area_old import Area
import json
from logging.handlers import RotatingFileHandler
from flask_sqlalchemy import get_debug_queries
from utils.nodetree import get_all_area
mod = Blueprint("core_area", __name__, url_prefix="/core/area", template_folder="templates", static_folder="static")
# 获取省的数据

@mod.route('/get_province/',methods=['GET','POST'])
def get_province():
    # 得到省得类目
    if request.method == 'POST' or 'GET':
        areas = Area.get_areas_by_level(1)
        tmp = {}
        data = []
        for item in areas:
            data.append({"id": item.id, "name": item.name, "parent_id": item.parent_id, "short_name": item.short_name,
                         "lng": item.lng, "lat": item.lat, "level": item.level,
                         "dis_order": item.dis_order, "status": item.status})
        tmp['success'] = 1
        tmp['error_code'] = None
        tmp['error_msg'] = None
        tmp['data'] = data
        return jsonify(tmp)
    else:
        return render_template('',**locals())


# 获取市的数据
@mod.route('/get_city/',methods=['GET','POST'])
def get_city():
    """
    {
    "id" : 120000
    }
    """
    #得到市的类目
    if request.method == 'POST':
        data = request.get_data()
        dict = json.loads(data.decode("utf-8"))
        province_id = dict['id']
        areas = Area.get_areas_by_parent_id(province_id)
        # print(province_id)
        tmp = {}
        data = []
        for item in areas:
            data.append({"id": +item.id, "name": item.name, "parent_id": item.parent_id, "short_name": item.short_name,
                         "lng": item.lng, "lat": item.lat, "level": item.level,
                         "dis_order": item.dis_order, "status": item.status})
        tmp['success'] = 1
        tmp['error_code'] = None
        tmp['error_msg'] = None
        tmp['data'] = data
        return jsonify(tmp)


# 获取区县的数据
@mod.route('/get_county/',methods=['GET','POST'])
def get_county():
    # 得到区县
    if request.method == 'POST':
        data = request.data
        dict11 = json.loads(data.decode("utf-8"))
        city_id = dict11['id']
        areas = Area.get_areas_by_parent_id(city_id)
        tmp = {}
        data = []
        for item in areas:
            data.append({"id": +item.id, "name": item.name, "parent_id": item.parent_id, "short_name": item.short_name,
                         "lng": item.lng, "lat": item.lat, "level": item.level,
                         "dis_order": item.dis_order, "status": item.status})
        tmp['success'] = 1
        tmp['error_code'] = None
        tmp['error_msg'] = None
        tmp['data'] = data
        return jsonify(tmp)


@mod.route('/get_town/',methods=['GET','POST'])
def get_town():
    if request.method == 'POST':
        data = request.data
        dict11 = json.loads(data.decode("utf-8"))
        county_id = dict11['id']
        areas = Area.get_areas_by_parent_id(county_id)
        tmp = {}
        data = []
        for item in areas:
            data.append({"id": +item.id, "name": item.name, "parent_id": item.parent_id, "short_name": item.short_name,
                         "lng": item.lng, "lat": item.lat, "level": item.level,
                         "dis_order": item.dis_order, "status": item.status})
        tmp['success'] = 1
        tmp['error_code'] = None
        tmp['error_msg'] = None
        tmp['data'] = data
        return jsonify(tmp)

# 获取全国的数据
@mod.route('/get_all_country/',methods=['GET','POST'])
def get_country():
    if request.method == 'POST' or 'GET':
        # 获取最高等级数据
        tmp = []
        paths = []
        all_tree_node = []
        module_root = Area.get_areas_by_parent_id(0)  # 得到所有的根节点
        #module_root [<Area '北京市'>, <Area '天津'>, <Area '河北省'>, <Area '山西省'>, <Area '内蒙古自治区'>, <Area '辽宁省'>]
        print('module_root', module_root)
        for item in module_root:
            t_list = []
            for t in Area.get_area_all():
                if t.path.split(',')[0] == str(item.id):
                    t_list.append(t)
            all_tree_node.append(t_list)
        rlist = []
        count = 0
        while count < len(module_root):
            rlist.append(get_all_area(all_tree_node[count], 0)[0])
            count += 1
        return jsonify(rlist)





